import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoPlayerItem extends StatefulWidget {
  final String videoUrl;

  final Function isPlayFunction;

  final bool isPlay;

  const VideoPlayerItem({
    Key? key,
    required this.videoUrl,
    required this.isPlay,
    required this.isPlayFunction,
  }) : super(key: key);

  @override
  _VideoPlayerItemState createState() => _VideoPlayerItemState();
}

class _VideoPlayerItemState extends State<VideoPlayerItem> {
  late VideoPlayerController videoPlayerController;

  bool isplays = true;

  void initState() {
    // TODO: implement initState
    super.initState();
    videoPlayerController = VideoPlayerController.asset(widget.videoUrl)     //network    assest
      ..initialize().then((value) {
        if (isplays) {
          videoPlayerController.play();
        } else {
          videoPlayerController.pause();
        }
        videoPlayerController.setVolume(10);
        videoPlayerController.removeListener(() {
          print("removeListener");
        });
        print(videoPlayerController.toString());
      });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    videoPlayerController.dispose(); //释放资源
  }

  @override
  Widget build(BuildContext context) {
    //获取屏幕宽度
    final size = MediaQuery.of(context).size;
    return Container(
      width: size.width,
      height: size.height,
      decoration: BoxDecoration(color: Colors.black),
      child: GestureDetector(
        onTap: () {
          print("点击了屏幕....");
          setState(() {
            isplays = !isplays;
          });
          if (isplays) {
            videoPlayerController.play();
            print("播放");
          } else {
            videoPlayerController.pause();
            print("暂停");
          }
          print("子组件给父组件传参");
          print(isplays);
          widget.isPlayFunction(isplays);
        },
        child: VideoPlayer(videoPlayerController),
      ),
    );
  }
}
